from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection
import logging
from pymilvus import utility

from pymilvus import connections, utility

# 配置连接参数
MILVUS_HOST = 'localhost'  # 替换为实际地址
MILVUS_PORT = '19530'  # 默认端口
USER = 'root'  # 默认用户名
PASSWORD = 'Milvus'  # 默认密码


def test_connection():
    try:
        # 建立连接
        connections.connect(
            alias="default",
            host=MILVUS_HOST,
            port=MILVUS_PORT,
            user=USER,
            password=PASSWORD
        )

        # 验证连接状态
        print(f"Server version: {utility.get_server_version()}")
        print(f"Connected collections: {utility.list_collections()}")

        return True
    except Exception as e:
        print(f"Connection failed: {str(e)}")
        return False
    finally:
        connections.disconnect("default")


if __name__ == "__main__":
    test_connection()
